package com.fuyun.rpc.factory;

import lombok.SneakyThrows;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author fuyun。
 * @Date 2025/8/22 11:34。
 * @Description: TODO。
 **/

public class SingletonFactory {

    private static final Map<Class<?>, Object> INSTANCE_CACHE = new ConcurrentHashMap<>();


    private SingletonFactory() {

    }

    @SneakyThrows
    public static <T> T getInstance(Class<T> clazz) {
        if (Objects.isNull(clazz)) {
            throw new IllegalArgumentException("clazz can not be null");
        }

        if (INSTANCE_CACHE.containsKey(clazz)) {
            return clazz.cast(INSTANCE_CACHE.get(clazz));
        }
        synchronized (SingletonFactory.class) {
            if (INSTANCE_CACHE.containsKey(clazz)) {
                return clazz.cast(INSTANCE_CACHE.get(clazz));
            }
            T t = clazz.getConstructor().newInstance();
            INSTANCE_CACHE.put(clazz, t);
            return t;
        }

    }
}
